package at.im.json;

import org.json.JSONException;
import org.json.JSONObject;

import at.im.utils.Box;
import at.im.utils.Entity;
import at.im.utils.EntityState;
import at.im.utils.Game;
import at.im.utils.GameBox;
import at.im.utils.GameBoxLayer;
import at.im.utils.GameBoxState;
import at.im.utils.GameEntity;
import at.im.utils.Sprite;
import at.im.utils.Tileset;
import at.im.utils.User;

public class JSONParser 
{
	public static Object parseJSON(JSONObject object, int type)
	{
		Object obj = null;
		
		switch(type)
		{
			case 0: 	obj = parse(object, new Box()); 				break;
			case 1: 	obj = parse(object, new Entity()); 				break;
			case 2: 	obj = parse(object, new EntityState()); 		break;
			case 3: 	obj = parse(object, new Game()); 				break;
			case 4: 	obj = parse(object, new GameBox()); 			break;
			case 5: 	obj = parse(object, new GameBoxLayer()); 		break;
			case 6: 	obj = parse(object, new GameBoxState());		break;	
			case 7: 	obj = parse(object, new GameEntity());			break;
			case 8: 	obj = parse(object, new Sprite());				break;
			case 9: 	obj = parse(object, new Tileset());				break;
			case 10:	obj = parse(object, new User());				break;
		}
		return obj;
	}
	
	private static Object parse(JSONObject object, EntityState entity)
	{
		try 
		{
			entity.setEntityStateId(object.getInt("entityStateId"));
			entity.setEntityId(object.getInt("entityId"));
			entity.setName(object.getString("name"));
			entity.setSpriteId(object.getInt("spriteId"));
			entity.setVisibility(object.getBoolean("isVisible"));
			entity.setTimeStamp(object.getString("changeDate"));
		    return entity;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, Entity entity)
	{
		try 
		{
			entity.setId(object.getInt("entityId"));
			entity.setTilesetId(object.getInt("tilesetId"));
			entity.setName(object.getString("name"));
			entity.setDefaultState(object.getInt("defaultStateId"));
			object.getInt("states");
			entity.setTimeStamp(object.getString("changeDate"));
		    return entity;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, Box box)
	{
		try 
		{
			box.setId(object.getInt("boxId"));
			box.setTilesetId(object.getInt("tilesetId"));
			box.setName(object.getString("name"));
			box.setSpriteId(object.getInt("spriteId"));
			box.setTimeStamp(object.getString("changeDate"));
		    return box;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, Game game)
	{
		try 
		{
			game.setGameId(object.getInt("gameId"));
			game.setName(object.getString("name"));
			game.setUserId(object.getInt("userId"));
			game.setTimeStamp(object.getString("changeDate"));
			game.setTilesetId(object.getInt("tileSetId"));
			game.setForegroundlayerId(object.getInt("foregroundLayerId"));
			game.setCenterlayoutId(object.getInt("centergroundlayerId"));
			game.setBackgroundlayerId(object.getInt("backgroundlayerId"));
			game.setBackgroundImage(object.getString("backgroundimage"));
			object.getInt("entites");
		    return game;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, GameBox entity)
	{
		try 
		{
			object.getString("gameBoxId");
			object.getString("gameBoxLayerId");
			object.getString("name");
			object.getString("posX");
			object.getString("posY");
			object.getString("defaultState");
			object.getString("states");
			object.getString("changeDate");
		    return new GameBox();
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, GameBoxLayer gameboxlayer)
	{
		try 
		{
			gameboxlayer.setGameboxlayerId(object.getInt("gameBoxLayerId"));
			gameboxlayer.setGameId(object.getInt("gameId"));
			object.getInt("gameBoxes");
			gameboxlayer.setTimeStamp(object.getString("changeDate"));
		    return gameboxlayer;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, GameBoxState gameboxstate)
	{
		try 
		{
			gameboxstate.setGameBoxStateId(object.getInt("gameBoxStateId"));
			gameboxstate.setGameBoxId(object.getInt("gameBoxId"));
			gameboxstate.setName(object.getString("name"));
			gameboxstate.setBoxId(object.getInt("boxId"));
			gameboxstate.setVisibility(object.getBoolean("isVisible"));
			gameboxstate.setTimeStamp(object.getString("changeDate"));
		    return gameboxstate;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, GameEntity gameentity)
	{
		try 
		{
			gameentity.setGameEntityId(object.getInt("gameEntityId"));
			gameentity.setGameId(object.getInt("gameId"));
			gameentity.setName(object.getString("name"));
			gameentity.setPosX(object.getInt("posX"));
			gameentity.setPosY(object.getInt("posY"));
			gameentity.setEntityId(object.getInt("entityId"));
			gameentity.setTimeStamp(object.getString("changeDate"));
		    return gameentity;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, User user)
	{
		try 
		{
			user.setId(object.getInt("userId"));
		    user.setName(object.getString("name"));
		    user.setEmail(object.getString("password"));
		    user.setRights(object.getInt("rights"));
		    user.setTimeStamp(object.getString("changeDate"));
		    return user;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, Sprite sprite)
	{
		try 
		{   
			sprite.setSpriteId(object.getInt("spriteId"));
			sprite.setTilesetId(object.getInt("tilesetId"));
			sprite.setImage(object.getString("image"));
			sprite.setFrames(object.getInt("frames"));
			sprite.setWidth(object.getInt("width"));
			sprite.setHeight(object.getInt("height"));
			sprite.setFPS(object.getInt("fps"));
			sprite.setTimeStamp(object.getString("changeDate"));
		    return sprite;
		} catch (JSONException e) 
		{
			return null;
		}
	}
	
	private static Object parse(JSONObject object, Tileset tileset)
	{
		try 
		{   
		    tileset.setTilesetId(object.getInt("tilesetId"));
		    tileset.setName(object.getString("name"));
		    tileset.setUserId(object.getInt("userId"));
		    tileset.setPublic(object.getBoolean("isPublic"));
		    tileset.setTimeStamp(object.getString("changeDate"));
		    object.getInt("boxes");
		    object.getInt("entities");
		    return tileset;
		} catch (JSONException e) 
		{
			return null;
		}
	}
}
